package com.soft.base.util;

import com.xiaoleilu.hutool.lang.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

/**
 * class
 *
 * @author user
 * @date 2020/06/05
 */
public class AesUtil {

    private static final String DEFAULT_KEY = "DefaultKeyForAes";
    private static final String DEFAULT_IV = "DefaultIV_ForAes";

    public static String decodeBase64(String data) {
        return decodeBase64(data, DEFAULT_KEY, DEFAULT_IV);
    }

    public static String decodeBase64(String data, String key, String iv) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.US_ASCII));
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(StandardCharsets.US_ASCII), "AES"), ivParameterSpec);
            byte[] decryptBytes = cipher.doFinal(Base64.decode(data));
            return new String(decryptBytes);
        } catch (Exception e) {
        }
        return null;
    }
}
